package com.project.webpage.controller;


import com.project.webpage.dto.*;
import com.project.webpage.service.*;
import com.project.webpage.util.RestResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/webpage/screen/")
public class ScreenController extends BaseController {

    @Resource
    CzxlRfidDayService czxlRfidDayService;
    @Resource
    CompanyOperationService companyOperationService;
    @Resource
    GjxlYbService gjxlYbService;
    @Resource
    LineAppealedService lineAppealedService;
    @Resource
    SkbService skbService;

    /**
     * 营运查询
     */
    @PostMapping("yingyun/page")
    public RestResult yingyun(@RequestBody SearchByRqParam param) {
        List<ScreenDailyDto> list = new ArrayList<>();
        param.setUid("admin");
        List<CzxlRfidDayDto> czxlRfidDayDtoList = czxlRfidDayService.getListForPage(param);
        for (CzxlRfidDayDto czxlRfidDayDto : czxlRfidDayDtoList) {
            ScreenDailyDto dto = new ScreenDailyDto();
            dto.setYhjc(czxlRfidDayDto.getYhjc());
            dto.setJt(czxlRfidDayDto.getJt());
            dto.setZcxls(czxlRfidDayDto.getZcxls());
            dto.setZcscxls(czxlRfidDayDto.getZcscxls());
            if (dto.getZcxls() > 0) {
                BigDecimal online = new BigDecimal(dto.getZcscxls()).divide(new BigDecimal(dto.getZcxls()), 1, RoundingMode.HALF_UP).multiply(new BigDecimal(100));
                dto.setXlzxl(String.valueOf(online));
            } else {
                dto.setXlzxl("0.0");
            }
            list.add(dto);
        }
        SearchByRqRangeParam rqRangeParam = new SearchByRqRangeParam();
        rqRangeParam.setStartDate(param.getRq());
        rqRangeParam.setEndDate(param.getRq());
        rqRangeParam.setUid("admin");
        List<CompanyDailyDto> companyDailyDtoList = companyOperationService.getDailyListForPage(rqRangeParam);
        for (ScreenDailyDto dto : list) {
            for (CompanyDailyDto companyDailyDto : companyDailyDtoList) {
                if (dto.getYhjc().equals(companyDailyDto.getYhjc())) {
                    dto.setPc(companyDailyDto.getPc());
                    dto.setPcl(companyDailyDto.getPcl());
                    dto.setPc_zaixian(companyDailyDto.getPc_zaixian());
                    dto.setPc_zaixian_ratio(companyDailyDto.getPc_zaixian_ratio());
                    dto.setBc(companyDailyDto.getBc());
                    dto.setBcl(companyDailyDto.getBcl());
                    dto.setSmbzdl(companyDailyDto.getSmbzdl());
                    dto.setShifts_ontime_ratio(companyDailyDto.getShifts_ontime_ratio());
                    dto.setGp_shifts_ontime_ratio(companyDailyDto.getGuapai_shifts_ontime_ratio());
                    if (companyDailyDto.getGaofeng_bc_ly() != null) {
                        dto.setGaofeng_bc_ly(companyDailyDto.getGaofeng_bc_ly());
                    }
                    dto.setDjg_ratio(companyDailyDto.getDjg_ratio());
                    break;
                }
            }
        }

        return RestResult.ok(list);
    }

    /**
     * 考核查询
     */
    @PostMapping("kaohe/page")
    public RestResult kaohe(@RequestBody SearchByMonthParam param) {
        param.setUid("admin");
        List<ScreenMonthlyDto> list = new ArrayList<>();
        List<CompanyMonthlyDto> companyMonthlyDtoList = companyOperationService.getMonthlyListForPage(param);
        for (CompanyMonthlyDto companyMonthlyDto : companyMonthlyDtoList) {
            ScreenMonthlyDto dto = new ScreenMonthlyDto();
            dto.setYhjc(companyMonthlyDto.getYhjc());
            dto.setJt(companyMonthlyDto.getJt());
            dto.setPc(companyMonthlyDto.getPc());
            dto.setPcl(companyMonthlyDto.getPcl());
            dto.setPc_zaixian(companyMonthlyDto.getPc_zaixian());
            dto.setPc_zaixian_ratio(companyMonthlyDto.getPc_zaixian_ratio());
            dto.setBc(companyMonthlyDto.getBc());
            dto.setBcl(companyMonthlyDto.getBcl());
            dto.setSmbzdl(companyMonthlyDto.getSmbzdl());
            dto.setShifts_ontime_ratio(companyMonthlyDto.getShifts_ontime_ratio());
            dto.setGp_shifts_ontime_ratio(companyMonthlyDto.getGuapai_shifts_ontime_ratio());
            if (companyMonthlyDto.getGaofeng_bc_ly() != null) {
                dto.setGaofeng_bc_ly(companyMonthlyDto.getGaofeng_bc_ly());
            }
            dto.setDjg_ratio(companyMonthlyDto.getDjg_ratio());
            list.add(dto);
        }
        List<CompanyNhDto> companyNhDtoList = gjxlYbService.getCompanyNhListForPage(param);
        for (ScreenMonthlyDto dto : list) {
            for (CompanyNhDto companyNhDto : companyNhDtoList) {
                if (dto.getYhjc().equals(companyNhDto.getYhjc())) {
                    dto.setQuantity(companyNhDto.getQuantity());
                    dto.setYylc(companyNhDto.getYylc());
                    dto.setNh_cy(companyNhDto.getNh_cy());
                    dto.setNh_dd(companyNhDto.getNh_dd());
                    break;
                }
            }
        }
        LineAppealedParam lineAppealedParam = new LineAppealedParam();
        lineAppealedParam.setMonth(param.getMonth());
        lineAppealedParam.setUid(param.getUid());
        lineAppealedParam.setType(0);
        List<LineAppealedDto> lineAppealedDtoList = lineAppealedService.getListForPage(lineAppealedParam);
        Map<String, List<LineAppealedDto>> lineAppealedListGroupMap = lineAppealedDtoList.stream().collect(Collectors.groupingBy(LineAppealedDto::getYhjc));
        for (ScreenMonthlyDto dto : list) {
            for (Map.Entry<String, List<LineAppealedDto>> entry : lineAppealedListGroupMap.entrySet()) {
                if (dto.getYhjc().equals(entry.getKey())) {
                    dto.setAppealed(entry.getValue().size());
                    List<LineAppealedDto> appealedDtoList = entry.getValue().stream().filter(item -> item.getAppealresult().equals("通过")).collect(Collectors.toList());
                    dto.setAppealed_allow(appealedDtoList.size());
                    break;
                }
            }
        }
        SkbParam skbParam = new SkbParam();
        skbParam.setUid("admin");
        skbParam.setState(1);
        skbParam.setSyzt(0);
        List<SkbDto> skbDtoList = skbService.getListForPage(skbParam);
        Map<String, List<SkbDto>> skbListGroupMap = skbDtoList.stream().collect(Collectors.groupingBy(SkbDto::getYhjc));
        for (ScreenMonthlyDto dto : list) {
            for (Map.Entry<String, List<SkbDto>> entry : skbListGroupMap.entrySet()) {
                if (dto.getYhjc().equals(entry.getKey())) {
                    dto.setSkb(entry.getValue().size());
                    break;
                }
            }
        }
        skbParam.setSyzt(1);
        List<SkbDto> skbDtoList1 = skbService.getListForPage(skbParam);
        Map<String, List<SkbDto>> skbListGroupMap1 = skbDtoList1.stream().collect(Collectors.groupingBy(SkbDto::getYhjc));
        for (ScreenMonthlyDto dto : list) {
            for (Map.Entry<String, List<SkbDto>> entry : skbListGroupMap1.entrySet()) {
                if (dto.getYhjc().equals(entry.getKey())) {
                    dto.setSkbbg(entry.getValue().size());
                    break;
                }
            }
        }
        return RestResult.ok(list);
    }
}
